from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from database import SessionLocal
import models, schemas

router = APIRouter()

def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@router.get("/", response_model=list[schemas.SoftwareSchema])
def list_softwares(db: Session = Depends(get_db)):
    return db.query(models.Software).all()

@router.get("/{software_id}", response_model=schemas.SoftwareSchema)
def get_software(software_id: int, db: Session = Depends(get_db)):
    item = db.query(models.Software).filter(models.Software.id == software_id).first()
    if not item:
        raise HTTPException(status_code=404, detail="Not found")
    return item
